package com.yxhl.zoume.core.user.ui.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yxhl.zoume.R;
import com.yxhl.zoume.common.adapter.UpPullLoadDataAdapter;
import com.yxhl.zoume.common.presenter.UpPullPresenter;
import com.yxhl.zoume.core.user.ui.activity.UserContainerActivity;
import com.yxhl.zoume.core.user.ui.info.UserCenterPageEnum;
import com.yxhl.zoume.data.http.model.base.BaseModel;
import com.yxhl.zoume.data.http.model.passenger.ZMPassenger;
import com.yxhl.zoume.utils.LOG;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by sugarya on 16/3/23.
 */
public class PassengerListAdapter extends UpPullLoadDataAdapter {

    private static final String TAG = "PassengerListAda";

    public PassengerListAdapter(Context mContext,
                                LinearLayoutManager mLayoutManager,
                                List<? extends BaseModel> dataList,
                                UpPullPresenter upPullPresenter) {
        super(mContext, mLayoutManager, dataList, upPullPresenter);
    }

    @Override
    public RecyclerView.ViewHolder onCustomCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_recycler_passenger, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onCustomBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            MyViewHolder myViewHolder = (MyViewHolder) holder;
            if (position >= 0 && position < mDataList.size()) {
                ZMPassenger zmPassenger = (ZMPassenger) mDataList.get(position);
                myViewHolder.onBindViewHolder(zmPassenger);
            }
        }
    }

    @Override
    public int getCustomItemCount() {
        return mDataList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.ll_item_passenger)
        LinearLayout revealLayout;
        @BindView(R.id.psger_info_checkbox)
        CheckBox checkBox;
        @BindView(R.id.psger_info_name_textview)
        TextView name;
        @BindView(R.id.psger_info_id_textview)
        TextView cardID;
        @BindView(R.id.psger_info_category_textview)
        TextView ticketKind;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(MyViewHolder.this, itemView);
            checkBox.setVisibility(View.GONE);
        }

        @OnClick(R.id.ll_item_passenger)
        void onItemClick(View view) {
            if (view != null) {
                int position = getLayoutPosition();
                LOG.e(TAG, "onItemClick position=" + position);
                if (mContext instanceof UserContainerActivity) {
                    UserContainerActivity containerActivity = (UserContainerActivity) mContext;
                    ZMPassenger zmPassenger = (ZMPassenger) mDataList.get(position);
                    containerActivity.setDirectionFlag(UserCenterPageEnum.UPDATE_PASSENGER);
                    containerActivity.exchangeUpdatePassengerFragmentByAdd(zmPassenger);
                }
            }
        }

        void onBindViewHolder(ZMPassenger zmPassenger) {
            if (zmPassenger != null) {
                setName(zmPassenger.getRealName());
                setIDCard(zmPassenger.getIdNumber());
                setTicketType(zmPassenger.getPsgerCategory());
            }
        }

        void setName(String s) {
            if (!TextUtils.isEmpty(s)) {
                name.setText(s);
            }
        }

        void setIDCard(String card) {
            if (!TextUtils.isEmpty(card)) {
                cardID.setText(card);
            }
        }

        void setTicketType(int type) {
            String typeStr;
            switch (type) {
                case ZMPassenger.ADULT:
                    typeStr = ZMPassenger.PassengerCategory.ADULT.getCategoryName();
                    break;
                case ZMPassenger.CHILD:
                    typeStr = ZMPassenger.PassengerCategory.CHILD.getCategoryName();
                    break;
                default:
                    typeStr = "";
            }
            ticketKind.setText(typeStr);
        }
    }


}
